﻿using System;
using System.Web.UI.WebControls;

namespace Mobile.Util
{
    public static class Common
    {
        public struct Periodo
        {
            private readonly string mAnno;
            private readonly string mMes;
            private readonly string mDia;

            public Periodo(string Anno, string Mes, string Dia)
            {
                mAnno = Anno;
                mMes = Mes;
                mDia = Dia;
            }

            public override string ToString()
            {
                return ToString(0);
            }

            public string ToString(short Formato)
            {
                return Formato == 1 ? string.Format("{0} {1}", months[Convert.ToInt16(mMes)-1].ToUpper(), mAnno) : string.Format("{0}{1}{2}", mAnno, mMes.PadLeft(2,Convert.ToChar("0")), mDia);
            }
        }

        private static readonly string[] months = {"Enero", "Febrero", "Marzo", "Abril", "Mayo", "Junio", "Julio", "Agosto", "Septiembre", "Octubre", "Noviembre","Diciembre"};
        
        public static void LoadYears(ref DropDownList ddl, int quantityYears)
        {
            LoadYears(ref ddl, DateTime.Now.Year, 5);
        }

        public static void LoadYears(ref DropDownList ddl, int startYear, int quantityYears)
        {
            ddl.Items.Clear();

            for (var i = startYear; i<=(startYear + quantityYears); i++)
                ddl.Items.Add(i.ToString());
        }

        public static string getMonthName(int month)
        {
            return months[month];
        }

        public static void CargaDropDownList(ref DropDownList ddl, object Tabla, string CampoTexto, string CampoValor, string ValorPorDefecto)
        {
            ddl.DataSource = Tabla;
            ddl.DataTextField = CampoTexto;
            ddl.DataValueField = CampoValor;
            ddl.DataBind();

            if (String.IsNullOrEmpty(ValorPorDefecto)) return;

            ddl.SelectedIndex = -1;

            ddl.Items.Insert(0, new ListItem(ValorPorDefecto, "-999", true));

            ddl.Items.FindByValue("-999").Selected = true;
        }

        public static string GetLast(string source, int tail_length)
        {
            return tail_length >= source.Length ? source : source.Substring(source.Length - tail_length);
        }

        public static string Escape(string input)
        {
            var result = input;

            if (result != null)
            {
                result = result.Replace("\\", @"\\");    // This needs to be done first!
                result = result.Replace("\"", @"\""");
                result = result.Replace("\a", @"\a");
                result = result.Replace("\b", @"\b");
                result = result.Replace("\f", @"\f");
                result = result.Replace("\n", @"\n");
                result = result.Replace("\r", @"\r");
                result = result.Replace("\t", @"\t");
                result = result.Replace("\v", @"\v");
                result = result.Replace("\0", @"\0");
            }

            return result;
        }

        public static int Aleatorio()
        {
            return Aleatorio(1, 2000);
        }

        private static int Aleatorio(int min, int max)
        {
            return new Random().Next(min, max);
        }
    }
}